package www.xpu.edu.app;

import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Activity_findjob extends AppCompatActivity implements ViewPager.OnPageChangeListener,OnClickListener {
    private ViewPager pager;
    private RadioGroup groups;
    private List<Fragment> fragments;
    private RadioButton tab1;
    private RadioButton tab2;
    private RadioButton tab3;
    private boolean temp=true;
   // private String tempstr="";
    //此变量储存身份标识符 默认为true->求职者 false->面试官
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_findjob);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        //找到控件
        pager = findViewById(R.id.fragment_1);
        groups = findViewById(R.id.tabs);
        fragments = new ArrayList<>();
        fragments.add(new Fragment1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());
        fragments.add(new Fragment4());
       /* fragments.add(new Fragment_2());
        fragments.add(new Fragment_3());*/
        //宣讲会
        findViewById(R.id.tab1).setOnClickListener(this);
        //求职招聘
        findViewById(R.id.tab2).setOnClickListener(this);
        //我的
        findViewById(R.id.tab3).setOnClickListener(this);
        //顶部的返回按钮和切换身份按钮
        findViewById(R.id.title_back).setOnClickListener(this);
        findViewById(R.id.title_change).setOnClickListener(this);
       // pager.setCurrentItem(3, false);

        //适配器



        pager.setAdapter(new FragmentPagerAdapter(this.getSupportFragmentManager()) {
                             @Override
                             public Fragment getItem(int i) {
                                 return fragments.get(i);
                             }

                             @Override
                             public int getCount() {
                                 return fragments.size();
                             }
                             //切换页面的监听器

                         }

        );
        //pager.setOnPageChangeListener(this);
       // groups.check(R.id.tab1);
    }


    @Override
    public void finish() {
        /*ViewGroup view =(ViewGroup)getWindow().getDecorView();
        view.removeAllViews();*/
        super.finish();
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
    switch (i)
    {
        case 0:
            groups.check(R.id.tab1);
            break;
        case 1:
            groups.check(R.id.tab2);
            break;
        case 2:
            groups.check(R.id.tab3);
            break;
         default:
            break;
    }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
//设置按钮的监听事件
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tab1:
                pager.setCurrentItem(0,true);
                break;
            case R.id.tab2:
                if(temp==true) {
                    pager.setCurrentItem(1, true);

                }
                if(temp==false)
                {
                    pager.setCurrentItem(3,true);
                }
                break;
            case R.id.tab3:
                pager.setCurrentItem(2,true);
                break;
            case R.id.title_back:
                Toast.makeText(Activity_findjob.this,"点击了返回按钮",Toast.LENGTH_SHORT).show();
                finish();
                break;
            case R.id.title_change:
                //true->求职者 false->面试官

                if(temp==false) {
                    Toast.makeText(Activity_findjob.this,"切换身份到面试官......",Toast.LENGTH_SHORT).show();
                    pager.setCurrentItem(1, true);

                }
                if(temp==true)
                {
                    Toast.makeText(Activity_findjob.this,"切换身份到求职者......",Toast.LENGTH_SHORT).show();
                    pager.setCurrentItem(3,true);
                }
                temp=!temp;

                break;

            default:
                break;
        }
    }
}

